'use strict';

const Controller = require('egg').Controller;
class NoticeController extends Controller {
  async getNoticeList() {
    const { ctx } = this;
    const res = await ctx.service.notice.getNoticeList();
    ctx.status = 200;
    ctx.body = {
      code: 0,
      data: res,
    };
  }

  async addNotice() {
    const { ctx } = this;
    try {
      ctx.validate({
        content: { type: 'string', reuqired: true },
        end_time: { type: 'string', reuqired: true },
      }, ctx.request.body);
      const data = ctx.request.body;
      data.end_time = parseInt(+(new Date(data.end_time)) / 1000);
      const res = await ctx.service.notice.addNotice(data);
      if (res.code === 0) {
        ctx.status = 200;
        ctx.body = {
          code: 0,
          data: res.data,
        };
      } else {
        ctx.status = 500;
        ctx.body = {
          code: -1,
          msg: '服务器出错',
        };
      }

    } catch (err) {
      ctx.status = 400;
      ctx.body = {
        code: 0,
        msg: '参数错误',
      };
    }

  }
  async updateNotice() {
    const { ctx } = this;
    try {
      ctx.validate({
        id: { type: 'int', required: true },
        content: { type: 'string', reuqired: true },
        end_time: { type: 'string', reuqired: true },
      }, ctx.request.body);
      const data = ctx.request.body;
      data.end_time = parseInt(+(new Date(data.end_time)) / 1000);
      const res = await ctx.service.notice.updateNotice(data);
      if (res.code === 0) {
        ctx.status = 200;
        ctx.body = {
          code: 0,
        };
      } else {
        ctx.status = 500;
        ctx.body = {
          code: -1,
          msg: '服务器出错',
        };
      }

    } catch (err) {
      ctx.status = 400;
      ctx.body = {
        code: 0,
        msg: '参数错误',
      };
    }
  }
}
module.exports = NoticeController;
